//
//  BaseSonViewController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-20.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "BaseSonViewController.h"
#import "DiscoveryCell.h"
#import "HomePageModel.h"
#import "DetailViewController.h"
@interface BaseSonViewController ()

@end

@implementation BaseSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr=[[NSMutableArray alloc]init];
    [self creatTableView];
}

-(void)creatRefreshViewWithID:(NSInteger)id
{
    __weak typeof (self) weakSelf = self;
    [weakSelf.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing=YES;
        weakSelf.limit=10;
        [weakSelf downDataWithUrl:[NSString stringWithFormat:categoryListUrl,(long)id,(long)weakSelf.limit] isRefresh:YES];
        
    }];
    [weakSelf.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class]  beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return;
        }
        weakSelf.isLoadMore=YES;
        weakSelf.limit+=10;
        [weakSelf downDataWithUrl:[NSString stringWithFormat:categoryListUrl,(long)id,(long)weakSelf.limit] isRefresh:YES];
    }];
}
- (void)endRefreshing {
    if (self.isRefreshing) {
        self.isRefreshing = NO;//标记刷新结束
        //正在刷新 就结束刷新
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultNone];
    }
    if (self.isLoadMore) {
        self.isLoadMore = NO;
        [self.tableView footerEndRefreshing];
    }
}
-(void)firstDownloadWithID:(NSInteger)cate
{
    self.limit=10;
    [self downDataWithUrl:[NSString stringWithFormat:categoryListUrl,(long)cate,(long)self.limit] isRefresh:NO];
}
-(void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,-44, kScreenSize.width, kScreenSize.height+44) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight=109;
    [self.tableView registerNib:[UINib nibWithNibName:@"DiscoveryCell" bundle:nil] forCellReuseIdentifier:@"DiscoveryCell"];
    [self.view addSubview:self.tableView];
}

-(void)downDataWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh
{
  //  NSString *url=[NSString stringWithFormat:categoryListUrl,d];
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    //只下载  不解析
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
                [weakSelf.dataArr removeAllObjects];
        
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *array=dict[@"result"];
            for (NSDictionary *dedict in array) {
                    HomePageModel *model=[[HomePageModel alloc]init];
                    [model setValue:dedict];
                        [weakSelf.dataArr addObject:model];
                  
                }
            }
  
            [weakSelf.tableView reloadData];
           [weakSelf endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载数据错误");
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoveryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"DiscoveryCell" forIndexPath:indexPath];
    HomePageModel *model=self.dataArr[indexPath.row];
    [cell setData:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *cnv=[[DetailViewController alloc]init];
    HomePageModel *model=self.dataArr[indexPath.row];
    cnv.url=model.url;
    cnv.articleId=model.articleId;
    cnv.imageUrl=model.imageUrl;
    cnv.Artitle=model.title;
    cnv.categryName=model.categryName;
    cnv.authorName=model.authorName;
    cnv.readCount=model.readCount;
    cnv.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:cnv animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
